home *** CD-ROM | disk | FTP | other *** search
/ Almathera Ten Pack 3: CDPD 3 / Almathera Ten on Ten - Disc 3: CDPD3.iso / scope / 001-025 / scopedisk4 / patch / pch.c < prev    next >
C/C++ Source or Header  |  1995-03-18  |  29KB  |  1,125 lines

  1. /* $Header: pch.c,v 2.0.1.6 87/06/04 16:18:13 lwall Exp $
  2.  *
  3.  * $Log:    pch.c,v $
  4.  * Revision 2.0.1.6  87/06/04  16:18:13  lwall
  5.  * pch_swap didn't swap p_bfake and p_efake.
  6.  * 
  7.  * Revision 2.0.1.5  87/01/30  22:47:42  lwall
  8.  * Improved responses to mangled patches.
  9.  * 
  10.  * Revision 2.0.1.4  87/01/05  16:59:53  lwall
  11.  * New-style context diffs caused double call to free().
  12.  * 
  13.  * Revision 2.0.1.3  86/11/14  10:08:33  lwall
  14.  * Fixed problem where a long pattern wouldn't grow the hunk.
  15.  * Also restored p_input_line when backtracking so error messages are right.
  16.  * 
  17.  * Revision 2.0.1.2  86/11/03  17:49:52  lwall
  18.  * New-style delete triggers spurious assertion error.
  19.  * 
  20.  * Revision 2.0.1.1  86/10/29  15:52:08  lwall
  21.  * Could falsely report new-style context diff.
  22.  * 
  23.  * Revision 2.0  86/09/17  15:39:37  lwall
  24.  * Baseline for netwide release.
  25.  * 
  26.  */
  27.  
  28. #include "EXTERN.h"
  29. #include "common.h"
  30. #include "util.h"
  31. #include "INTERN.h"
  32. #include "pch.h"
  33.  
  34. /* Patch (diff listing) abstract type. */
  35.  
  36. static long p_filesize;            /* size of the patch file */
  37. static LINENUM p_first;            /* 1st line number */
  38. static LINENUM p_newfirst;        /* 1st line number of replacement */
  39. static LINENUM p_ptrn_lines;        /* # lines in pattern */
  40. static LINENUM p_repl_lines;        /* # lines in replacement text */
  41. static LINENUM p_end = -1;        /* last line in hunk */
  42. static LINENUM p_max;            /* max allowed value of p_end */
  43. static LINENUM p_context = 3;        /* # of context lines */
  44. static LINENUM p_input_line = 0;    /* current line # from patch file */
  45. static char **p_line = Null(char**);    /* the text of the hunk */
  46. static short *p_len = Null(short*);    /* length of each line */
  47. static char *p_char = Nullch;        /* +, -, and ! */
  48. static int hunkmax = INITHUNKMAX;    /* size of above arrays to begin with */
  49. static int p_indent;            /* indent to patch */
  50. static LINENUM p_base;            /* where to intuit this time */
  51. static LINENUM p_bline;            /* line # of p_base */
  52. static LINENUM p_start;            /* where intuit found a patch */
  53. static LINENUM p_sline;            /* and the line number for it */
  54. static LINENUM p_hunk_beg;        /* line number of current hunk */
  55. static LINENUM p_efake = -1;        /* end of faked up lines--don't free */
  56. static LINENUM p_bfake = -1;        /* beg of faked up lines */
  57.  
  58. /* Prepare to look for the next patch in the patch file. */
  59.  
  60. void
  61. re_patch()
  62. {
  63.     p_first = Nulline;
  64.     p_newfirst = Nulline;
  65.     p_ptrn_lines = Nulline;
  66.     p_repl_lines = Nulline;
  67.     p_end = (LINENUM)-1;
  68.     p_max = Nulline;
  69.     p_indent = 0;
  70. }
  71.  
  72. /* Open the patch file at the beginning of time. */
  73.  
  74. void
  75. open_patch_file(filename)
  76. char *filename;
  77. {
  78.     if (filename == Nullch || !*filename || strEQ(filename, "-")) {
  79.     pfp = fopen(TMPPATNAME, "w");
  80.     if (pfp == Nullfp)
  81.         fatal2("patch: can't create %s.\n", TMPPATNAME);
  82.     while (fgets(buf, sizeof buf, stdin) != Nullch)
  83.         fputs(buf, pfp);
  84.     Fclose(pfp);
  85.     filename = TMPPATNAME;
  86.     }
  87.     pfp = fopen(filename, "r");
  88.     if (pfp == Nullfp)
  89.     fatal2("patch file %s not found\n", filename);
  90. #ifdef AMIGA
  91.     if(dfindOne(&filestat, filename, 0))
  92.     fatal2("dfindOne on file %s failed\n", filename);
  93.     p_filesize = filestat.fib_Size;
  94. #else
  95.     Fstat(fileno(pfp), &filestat);
  96.     p_filesize = filestat.st_size;
  97. #endif
  98.     next_intuit_at(0L,1L);            /* start at the beginning */
  99.     set_hunkmax();
  100. }
  101.  
  102. /* Make sure our dynamically realloced tables are malloced to begin with. */
  103.  
  104. void
  105. set_hunkmax()
  106. {
  107. #ifndef lint
  108.     if (p_line == Null(char**))
  109.     p_line = (char**) malloc((MEM)hunkmax * sizeof(char *));
  110.     if (p_len == Null(short*))
  111.     p_len  = (short*) malloc((MEM)hunkmax * sizeof(short));
  112. #endif
  113.     if (p_char == Nullch)
  114.     p_char = (char*)  malloc((MEM)hunkmax * sizeof(char));
  115. }
  116.  
  117. /* Enlarge the arrays containing the current hunk of patch. */
  118.  
  119. void
  120. grow_hunkmax()
  121. {
  122.     hunkmax *= 2;
  123.     /* 
  124.      * Note that on most systems, only the p_line array ever gets fresh memory
  125.      * since p_len can move into p_line's old space, and p_char can move into
  126.      * p_len's old space.  Not on PDP-11's however.  But it doesn't matter.
  127.      */
  128.     assert(p_line != Null(char**) && p_len != Null(short*) && p_char != Nullch);
  129. #ifndef lint
  130.     p_line = (char**) realloc((char*)p_line, (MEM)hunkmax * sizeof(char *));
  131.     p_len  = (short*) realloc((char*)p_len,  (MEM)hunkmax * sizeof(short));
  132.     p_char = (char*)  realloc((char*)p_char, (MEM)hunkmax * sizeof(char));
  133. #endif
  134.     if (p_line != Null(char**) && p_len != Null(short*) && p_char != Nullch)
  135.     return;
  136.     if (!using_plan_a)
  137.     fatal1("patch: out of memory (grow_hunkmax)\n");
  138.     out_of_mem = TRUE;        /* whatever is null will be allocated again */
  139.                 /* from within plan_a(), of all places */
  140. }
  141.  
  142. /* True if the remainder of the patch file contains a diff of some sort. */
  143.  
  144. bool
  145. there_is_another_patch()
  146. {
  147.     if (p_base != 0L && p_base >= p_filesize) {
  148.     if (verbose)
  149.         say1("done\n");
  150.     return FALSE;
  151.     }
  152.     if (verbose)
  153.     say1("Hmm...");
  154.     diff_type = intuit_diff_type();
  155.     if (!diff_type) {
  156.     if (p_base != 0L) {
  157.         if (verbose)
  158.         say1("  Ignoring the trailing garbage.\ndone\n");
  159.     }
  160.     else
  161.         say1("  I can't seem to find a patch in there anywhere.\n");
  162.     return FALSE;
  163.     }
  164.     if (verbose)
  165.     say3("  %sooks like %s to me...\n",
  166.         (p_base == 0L ? "L" : "The next patch l"),
  167.         diff_type == CONTEXT_DIFF ? "a context diff" :
  168.         diff_type == NEW_CONTEXT_DIFF ? "a new-style context diff" :
  169.         diff_type == NORMAL_DIFF ? "a normal diff" :
  170.         "an ed script" );
  171.     if (p_indent && verbose)
  172.     say3("(Patch is indented %d space%s.)\n", p_indent, p_indent==1?"":"s");
  173.     skip_to(p_start,p_sline);
  174.     while (filearg[0] == Nullch) {
  175.     if (force) {
  176.         say1("No file to patch.  Skipping...\n");
  177.         filearg[0] = savestr(bestguess);
  178.         return TRUE;
  179.     }
  180. #ifdef AMIGA
  181.     fileRequest("File to patch");
  182. #else
  183.     ask1("File to patch: ");
  184. #endif
  185.     if (*buf != '\n') {
  186.         if (bestguess)
  187.         free(bestguess);
  188.         bestguess = savestr(buf);
  189.         filearg[0] = fetchname(buf, 0, FALSE);
  190.     }
  191.     if (filearg[0] == Nullch) {
  192. #ifdef AMIGA
  193.         ask1("No file found--skip this patch?");
  194. #else
  195.         ask1("No file found--skip this patch? [n] ");
  196. #endif
  197.         if (*buf != 'y') {
  198.         continue;
  199.         }
  200.         if (verbose)
  201.         say1("Skipping patch...\n");
  202.         filearg[0] = fetchname(bestguess, 0, TRUE);
  203.         skip_rest_of_patch = TRUE;
  204.         return TRUE;
  205.     }
  206.     }
  207.     return TRUE;
  208. }
  209.  
  210. /* Determine what kind of diff is in the remaining part of the patch file. */
  211.  
  212. int
  213. intuit_diff_type()
  214. {
  215.     Reg4 long this_line = 0;
  216.     Reg5 long previous_line;
  217.     Reg6 long first_command_line = -1;
  218.     long fcl_line;
  219.     Reg7 bool last_line_was_command = FALSE;
  220.     Reg8 bool this_is_a_command = FALSE;
  221.     Reg9 bool stars_last_line = FALSE;
  222.     Reg10 bool stars_this_line = FALSE;
  223.     Reg3 int indent;
  224.     Reg1 char *s;
  225.     Reg2 char *t;
  226.     char *indtmp = Nullch;
  227.     char *oldtmp = Nullch;
  228.     char *newtmp = Nullch;
  229.     char *indname = Nullch;
  230.     char *oldname = Nullch;
  231.     char *newname = Nullch;
  232.     Reg11 int retval;
  233.     bool no_filearg = (filearg[0] == Nullch);
  234.  
  235.     ok_to_create_file = FALSE;
  236.     Fseek(pfp, p_base, 0);
  237.     p_input_line = p_bline - 1;
  238.     for (;;) {
  239.     previous_line = this_line;
  240.     last_line_was_command = this_is_a_command;
  241.     stars_last_line = stars_this_line;
  242.     this_line = ftell(pfp);
  243.     indent = 0;
  244.     p_input_line++;
  245.     if (fgets(buf, sizeof buf, pfp) == Nullch) {
  246.         if (first_command_line >= 0L) {
  247.                     /* nothing but deletes!? */
  248.         p_start = first_command_line;
  249.         p_sline = fcl_line;
  250.         retval = ED_DIFF;
  251.         goto scan_exit;
  252.         }
  253.         else {
  254.         p_start = this_line;
  255.         p_sline = p_input_line;
  256.         retval = 0;
  257.         goto scan_exit;
  258.         }
  259.     }
  260.     for (s = buf; *s == ' ' || *s == '\t'; s++) {
  261.         if (*s == '\t')
  262.         indent += 8 - (indent % 8);
  263.         else
  264.         indent++;
  265.     }
  266.     for (t=s; isdigit(*t) || *t == ','; t++) ; 
  267.     this_is_a_command = (isdigit(*s) &&
  268.       (*t == 'd' || *t == 'c' || *t == 'a') );
  269.     if (first_command_line < 0L && this_is_a_command) { 
  270.         first_command_line = this_line;
  271.         fcl_line = p_input_line;
  272.         p_indent = indent;        /* assume this for now */
  273.     }
  274.     if (!stars_last_line && strnEQ(s, "*** ", 4))
  275.         oldtmp = savestr(s+4);
  276.     else if (strnEQ(s, "--- ", 4))
  277.         newtmp = savestr(s+4);
  278.     else if (strnEQ(s, "Index:", 6))
  279.         indtmp = savestr(s+6);
  280.     else if (strnEQ(s, "Prereq:", 7)) {
  281.         for (t=s+7; isspace(*t); t++) ;
  282.         revision = savestr(t);
  283.         for (t=revision; *t && !isspace(*t); t++) ;
  284.         *t = '\0';
  285.         if (!*revision) {
  286.         free(revision);
  287.         revision = Nullch;
  288.         }
  289.     }
  290.     if ((!diff_type || diff_type == ED_DIFF) &&
  291.       first_command_line >= 0L &&
  292.       strEQ(s, ".\n") ) {
  293.         p_indent = indent;
  294.         p_start = first_command_line;
  295.         p_sline = fcl_line;
  296.         retval = ED_DIFF;
  297.         goto scan_exit;
  298.     }
  299.     stars_this_line = strnEQ(s, "********", 8);
  300.     if ((!diff_type || diff_type == CONTEXT_DIFF) && stars_last_line &&
  301.          strnEQ(s, "*** ", 4)) {
  302.         if (!atol(s+4))
  303.         ok_to_create_file = TRUE;
  304.         /* if this is a new context diff the character just before */
  305.         /* the newline is a '*'. */
  306.         while (*s != '\n')
  307.         s++;
  308.         p_indent = indent;
  309.         p_start = previous_line;
  310.         p_sline = p_input_line - 1;
  311.         retval = (*(s-1) == '*' ? NEW_CONTEXT_DIFF : CONTEXT_DIFF);
  312.         goto scan_exit;
  313.     }
  314.     if ((!diff_type || diff_type == NORMAL_DIFF) && 
  315.       last_line_was_command &&
  316.       (strnEQ(s, "< ", 2) || strnEQ(s, "> ", 2)) ) {
  317.         p_start = previous_line;
  318.         p_sline = p_input_line - 1;
  319.         p_indent = indent;
  320.         retval = NORMAL_DIFF;
  321.         goto scan_exit;
  322.     }
  323.     }
  324.   scan_exit:
  325.     if (no_filearg) {
  326.     if (indtmp != Nullch)
  327.         indname = fetchname(indtmp, strippath, ok_to_create_file);
  328.     if (oldtmp != Nullch)
  329.         oldname = fetchname(oldtmp, strippath, ok_to_create_file);
  330.     if (newtmp != Nullch)
  331.         newname = fetchname(newtmp, strippath, ok_to_create_file);
  332.     if (oldname && newname) {
  333.         if (strlen(oldname) < strlen(newname))
  334.         filearg[0] = savestr(oldname);
  335.         else
  336.         filearg[0] = savestr(newname);
  337.     }
  338.     else if (oldname)
  339.         filearg[0] = savestr(oldname);
  340.     else if (newname)
  341.         filearg[0] = savestr(newname);
  342.     else if (indname)
  343.         filearg[0] = savestr(indname);
  344.     }
  345.     if (bestguess) {
  346.     free(bestguess);
  347.     bestguess = Nullch;
  348.     }
  349.     if (filearg[0] != Nullch)
  350.     bestguess = savestr(filearg[0]);
  351.     else if (indtmp != Nullch)
  352.     bestguess = fetchname(indtmp, strippath, TRUE);
  353.     else {
  354.     if (oldtmp != Nullch)
  355.         oldname = fetchname(oldtmp, strippath, TRUE);
  356.     if (newtmp != Nullch)
  357.         newname = fetchname(newtmp, strippath, TRUE);
  358.     if (oldname && newname) {
  359.         if (strlen(oldname) < strlen(newname))
  360.         bestguess = savestr(oldname);
  361.         else
  362.         bestguess = savestr(newname);
  363.     }
  364.     else if (oldname)
  365.         bestguess = savestr(oldname);
  366.     else if (newname)
  367.         bestguess = savestr(newname);
  368.     }
  369.     if (indtmp != Nullch)
  370.     free(indtmp);
  371.     if (oldtmp != Nullch)
  372.     free(oldtmp);
  373.     if (newtmp != Nullch)
  374.     free(newtmp);
  375.     if (indname != Nullch)
  376.     free(indname);
  377.     if (oldname != Nullch)
  378.     free(oldname);
  379.     if (newname != Nullch)
  380.     free(newname);
  381.     return retval;
  382. }
  383.  
  384. /* Remember where this patch ends so we know where to start up again. */
  385.  
  386. void
  387. next_intuit_at(file_pos,file_line)
  388. long file_pos;
  389. long file_line;
  390. {
  391.     p_base = file_pos;
  392.     p_bline = file_line;
  393. }
  394.  
  395. /* Basically a verbose fseek() to the actual diff listing. */
  396.  
  397. void
  398. skip_to(file_pos,file_line)
  399. long file_pos;
  400. long file_line;
  401. {
  402.     char *ret;
  403.  
  404.     assert(p_base <= file_pos);
  405.     if (verbose && p_base < file_pos) {
  406.     Fseek(pfp, p_base, 0);
  407.     say1("The text leading up to this was:\n--------------------------\n");
  408.     while (ftell(pfp) < file_pos) {
  409.         ret = fgets(buf, sizeof buf, pfp);
  410.         assert(ret != Nullch);
  411.         say2("|%s", buf);
  412.     }
  413.     say1("--------------------------\n");
  414.     }
  415.     else
  416.     Fseek(pfp, file_pos, 0);
  417.     p_input_line = file_line - 1;
  418. }
  419.  
  420. /* True if there is more of the current diff listing to process. */
  421.  
  422. bool
  423. another_hunk()
  424. {
  425.     Reg1 char *s;
  426.     Reg8 char *ret;
  427.     Reg2 int context = 0;
  428.  
  429.     while (p_end >= 0) {
  430.     if (p_end == p_efake)
  431.         p_end = p_bfake;        /* don't free twice */
  432.     else
  433.         free(p_line[p_end]);
  434.     p_end--;
  435.     }
  436.     assert(p_end == -1);
  437.     p_efake = -1;
  438.  
  439.     p_max = hunkmax;            /* gets reduced when --- found */
  440.     if (diff_type == CONTEXT_DIFF || diff_type == NEW_CONTEXT_DIFF) {
  441.     long line_beginning = ftell(pfp);
  442.                     /* file pos of the current line */
  443.     LINENUM repl_beginning = 0;    /* index of --- line */
  444.     Reg4 LINENUM fillcnt = 0;    /* #lines of missing ptrn or repl */
  445.     Reg5 LINENUM fillsrc;        /* index of first line to copy */
  446.     Reg6 LINENUM filldst;        /* index of first missing line */
  447.     bool ptrn_spaces_eaten = FALSE;    /* ptrn was slightly misformed */
  448.     Reg9 bool repl_could_be_missing = TRUE;
  449.                     /* no + or ! lines in this hunk */
  450.     bool repl_missing = FALSE;    /* we are now backtracking */
  451.     long repl_backtrack_position = 0;
  452.                     /* file pos of first repl line */
  453.     LINENUM repl_patch_line;    /* input line number for same */
  454.     Reg7 LINENUM ptrn_copiable = 0;
  455.                     /* # of copiable lines in ptrn */
  456.  
  457.     ret = pgets(buf, sizeof buf, pfp);
  458.     p_input_line++;
  459.     if (ret == Nullch || strnNE(buf, "********", 8)) {
  460.         next_intuit_at(line_beginning,p_input_line);
  461.         return FALSE;
  462.     }
  463.     p_context = 100;
  464.     p_hunk_beg = p_input_line + 1;
  465.     while (p_end < p_max) {
  466.         line_beginning = ftell(pfp);
  467.         ret = pgets(buf, sizeof buf, pfp);
  468.         p_input_line++;
  469.         if (ret == Nullch) {
  470.         if (p_max - p_end < 4)
  471.             Strcpy(buf, "  \n");  /* assume blank lines got chopped */
  472.         else {
  473.             if (repl_beginning && repl_could_be_missing) {
  474.             repl_missing = TRUE;
  475.             goto hunk_done;
  476.             }
  477.             fatal1("Unexpected end of file in patch.\n");
  478.         }
  479.         }
  480.         p_end++;
  481.         assert(p_end < hunkmax);
  482.         p_char[p_end] = *buf;
  483.         p_line[p_end] = Nullch;
  484.         switch (*buf) {
  485.         case '*':
  486.         if (strnEQ(buf, "********", 8)) {
  487.             if (repl_beginning && repl_could_be_missing) {
  488.             repl_missing = TRUE;
  489.             goto hunk_done;
  490.             }
  491.             else
  492.             fatal2("Unexpected end of hunk at line %ld.\n",
  493.                 p_input_line);
  494.         }
  495.         if (p_end != 0) {
  496.             if (repl_beginning && repl_could_be_missing) {
  497.             repl_missing = TRUE;
  498.             goto hunk_done;
  499.             }
  500.             fatal3("Unexpected *** at line %ld: %s", p_input_line, buf);
  501.         }
  502.         context = 0;
  503.         p_line[p_end] = savestr(buf);
  504.         if (out_of_mem) {
  505.             p_end--;
  506.             return FALSE;
  507.         }
  508.         for (s=buf; *s && !isdigit(*s); s++) ;
  509.         if (!*s)
  510.             goto malformed;
  511.         p_first = (LINENUM) atol(s);
  512.         while (isdigit(*s)) s++;
  513.         if (*s == ',') {
  514.             for (; *s && !isdigit(*s); s++) ;
  515.             if (!*s)
  516.             goto malformed;
  517.             p_ptrn_lines = ((LINENUM)atol(s)) - p_first + 1;
  518.         }
  519.         else if (p_first)
  520.             p_ptrn_lines = 1;
  521.         else {
  522.             p_ptrn_lines = 0;
  523.             p_first = 1;
  524.         }
  525.         p_max = p_ptrn_lines + 6;    /* we need this much at least */
  526.         while (p_max >= hunkmax)
  527.             grow_hunkmax();
  528.         p_max = hunkmax;
  529.         break;
  530.         case '-':
  531.         if (buf[1] == '-') {
  532.             if (repl_beginning ||
  533.             (p_end != p_ptrn_lines + 1 + (p_char[p_end-1] == '\n')))
  534.             {
  535.             if (p_end == 1) {
  536.                 /* `old' lines were omitted - set up to fill */
  537.                 /* them in from 'new' context lines. */
  538.                 p_end = p_ptrn_lines + 1;
  539.                 fillsrc = p_end + 1;
  540.                 filldst = 1;
  541.                 fillcnt = p_ptrn_lines;
  542.             }
  543.             else {
  544.                 if (repl_beginning) {
  545.                 if (repl_could_be_missing){
  546.                     repl_missing = TRUE;
  547.                     goto hunk_done;
  548.                 }
  549.                 fatal3("Duplicate \"---\" at line %ld--check line numbers at line %ld.\n",
  550.                     p_input_line, p_hunk_beg + repl_beginning);
  551.                 }
  552.                 else {
  553.                 fatal4("%s \"---\" at line %ld--check line numbers at line %ld.\n",
  554.                     (p_end <= p_ptrn_lines
  555.                     ? "Premature"
  556.                     : "Overdue" ),
  557.                     p_input_line, p_hunk_beg);
  558.                 }
  559.             }
  560.             }
  561.             repl_beginning = p_end;
  562.             repl_backtrack_position = ftell(pfp);
  563.             repl_patch_line = p_input_line;
  564.             p_line[p_end] = savestr(buf);
  565.             if (out_of_mem) {
  566.             p_end--;
  567.             return FALSE;
  568.             }
  569.             p_char[p_end] = '=';
  570.             for (s=buf; *s && !isdigit(*s); s++) ;
  571.             if (!*s)
  572.             goto malformed;
  573.             p_newfirst = (LINENUM) atol(s);
  574.             while (isdigit(*s)) s++;
  575.             if (*s == ',') {
  576.             for (; *s && !isdigit(*s); s++) ;
  577.             if (!*s)
  578.                 goto malformed;
  579.             p_repl_lines = ((LINENUM)atol(s)) - p_newfirst + 1;
  580.             }
  581.             else if (p_newfirst)
  582.             p_repl_lines = 1;
  583.             else {
  584.             p_repl_lines = 0;
  585.             p_newfirst = 1;
  586.             }
  587.             p_max = p_repl_lines + p_end;
  588.             if (p_max > MAXHUNKSIZE)
  589.             fatal4("Hunk too large (%ld lines) at line %ld: %s",
  590.                   p_max, p_input_line, buf);
  591.             while (p_max >= hunkmax)
  592.             grow_hunkmax();
  593.             if (p_repl_lines != ptrn_copiable)
  594.             repl_could_be_missing = FALSE;
  595.             break;
  596.         }
  597.         goto change_line;
  598.         case '+':  case '!':
  599.         repl_could_be_missing = FALSE;
  600.           change_line:
  601.         if (!isspace(buf[1]) && buf[1] != '>' && buf[1] != '<' &&
  602.           repl_beginning && repl_could_be_missing) {
  603.             repl_missing = TRUE;
  604.             goto hunk_done;
  605.         }
  606.         if (context > 0) {
  607.             if (context < p_context)
  608.             p_context = context;
  609.             context = -1000;
  610.         }
  611.         p_line[p_end] = savestr(buf+2);
  612.         if (out_of_mem) {
  613.             p_end--;
  614.             return FALSE;
  615.         }
  616.         break;
  617.         case '\t': case '\n':    /* assume the 2 spaces got eaten */
  618.         if (repl_beginning && repl_could_be_missing &&
  619.           (!ptrn_spaces_eaten || diff_type == NEW_CONTEXT_DIFF) ) {
  620.             repl_missing = TRUE;
  621.             goto hunk_done;
  622.         }
  623.         p_line[p_end] = savestr(buf);
  624.         if (out_of_mem) {
  625.             p_end--;
  626.             return FALSE;
  627.         }
  628.         if (p_end != p_ptrn_lines + 1) {
  629.             ptrn_spaces_eaten |= (repl_beginning != 0);
  630.             context++;
  631.             if (!repl_beginning)
  632.             ptrn_copiable++;
  633.             p_char[p_end] = ' ';
  634.         }
  635.         break;
  636.         case ' ':
  637.         if (!isspace(buf[1]) &&
  638.           repl_beginning && repl_could_be_missing) {
  639.             repl_missing = TRUE;
  640.             goto hunk_done;
  641.         }
  642.         context++;
  643.         if (!repl_beginning)
  644.             ptrn_copiable++;
  645.         p_line[p_end] = savestr(buf+2);
  646.         if (out_of_mem) {
  647.             p_end--;
  648.             return FALSE;
  649.         }
  650.         break;
  651.         default:
  652.         if (repl_beginning && repl_could_be_missing) {
  653.             repl_missing = TRUE;
  654.             goto hunk_done;
  655.         }
  656.         goto malformed;
  657.         }
  658.         /* set up p_len for strncmp() so we don't have to */
  659.         /* assume null termination */
  660.         if (p_line[p_end])
  661.         p_len[p_end] = strlen(p_line[p_end]);
  662.         else
  663.         p_len[p_end] = 0;
  664.     }
  665.     
  666.     hunk_done:
  667.     if (p_end >=0 && !repl_beginning)
  668.         fatal2("No --- found in patch at line %ld\n", pch_hunk_beg());
  669.  
  670.     if (repl_missing) {
  671.         
  672.         /* reset state back to just after --- */
  673.         p_input_line = repl_patch_line;
  674.         for (p_end--; p_end > repl_beginning; p_end--)
  675.         free(p_line[p_end]);
  676.         Fseek(pfp, repl_backtrack_position, 0);
  677.         
  678.         /* redundant 'new' context lines were omitted - set */
  679.         /* up to fill them in from the old file context */
  680.         fillsrc = 1;
  681.         filldst = repl_beginning+1;
  682.         fillcnt = p_repl_lines;
  683.         p_end = p_max;
  684.     }
  685.  
  686.     if (diff_type == CONTEXT_DIFF &&
  687.       (fillcnt || (p_first > 1 && ptrn_copiable > 2*p_context)) ) {
  688.         if (verbose)
  689.         say1(
  690. "(Fascinating--this is really a new-style context diff but without the ");
  691.         say1(
  692. "telltale\nextra asterisks on the *** line that usually indicate the new");
  693.         say1("style...)\n");
  694.         diff_type = NEW_CONTEXT_DIFF;
  695.     }
  696.     
  697.     /* if there were omitted context lines, fill them in now */
  698.     if (fillcnt) {
  699.         p_bfake = filldst;        /* remember where not to free() */
  700.         p_efake = filldst + fillcnt - 1;
  701.         while (fillcnt-- > 0) {
  702.         while (fillsrc <= p_end && p_char[fillsrc] != ' ')
  703.             fillsrc++;
  704.         if (fillsrc > p_end)
  705.             fatal2("Replacement text or line numbers mangled in hunk at line %ld\n",
  706.             p_hunk_beg);
  707.         p_line[filldst] = p_line[fillsrc];
  708.         p_char[filldst] = p_char[fillsrc];
  709.         p_len[filldst] = p_len[fillsrc];
  710.         fillsrc++; filldst++;
  711.         }
  712.         while (fillsrc <= p_end && fillsrc != repl_beginning &&
  713.           p_char[fillsrc] != ' ')
  714.         fillsrc++;
  715. #ifdef DEBUGGING
  716.         if (debug & 64)
  717.         printf("fillsrc %ld, filldst %ld, rb %ld, e+1 %ld\n",
  718.             fillsrc,filldst,repl_beginning,p_end+1);
  719. #endif
  720.         assert(fillsrc==p_end+1 || fillsrc==repl_beginning);
  721.         assert(filldst==p_end+1 || filldst==repl_beginning);
  722.     }
  723.     }
  724.     else {                /* normal diff--fake it up */
  725.     char hunk_type;
  726.     Reg3 int i;
  727.     LINENUM lMin, lMax;
  728.     long line_beginning = ftell(pfp);
  729.  
  730.     p_context = 0;
  731.     ret = pgets(buf, sizeof buf, pfp);
  732.     p_input_line++;
  733.     if (ret == Nullch || !isdigit(*buf)) {
  734.         next_intuit_at(line_beginning,p_input_line);
  735.         return FALSE;
  736.     }
  737.     p_first = (LINENUM)atol(buf);
  738.     for (s=buf; isdigit(*s); s++) ;
  739.     if (*s == ',') {
  740.         p_ptrn_lines = (LINENUM)atol(++s) - p_first + 1;
  741.         while (isdigit(*s)) s++;
  742.     }
  743.     else
  744.         p_ptrn_lines = (*s != 'a');
  745.     hunk_type = *s;
  746.     if (hunk_type == 'a')
  747.         p_first++;            /* do append rather than insert */
  748.     lMin = (LINENUM)atol(++s);
  749.     for (; isdigit(*s); s++) ;
  750.     if (*s == ',')
  751.         lMax = (LINENUM)atol(++s);
  752.     else
  753.         lMax = lMin;
  754.     if (hunk_type == 'd')
  755.         lMin++;
  756.     p_end = p_ptrn_lines + 1 + lMax - lMin + 1;
  757.     if (p_end > MAXHUNKSIZE)
  758.         fatal4("Hunk too large (%ld lines) at line %ld: %s",
  759.           p_end, p_input_line, buf);
  760.     while (p_end >= hunkmax)
  761.         grow_hunkmax();
  762.     p_newfirst = lMin;
  763.     p_repl_lines = lMax - lMin + 1;
  764.     Sprintf(buf, "*** %ld,%ld\n", p_first, p_first + p_ptrn_lines - 1);
  765.     p_line[0] = savestr(buf);
  766.     if (out_of_mem) {
  767.         p_end = -1;
  768.         return FALSE;
  769.     }
  770.     p_char[0] = '*';
  771.     for (i=1; i<=p_ptrn_lines; i++) {
  772.         ret = pgets(buf, sizeof buf, pfp);
  773.         p_input_line++;
  774.         if (ret == Nullch)
  775.         fatal2("Unexpected end of file in patch at line %ld.\n",
  776.           p_input_line);
  777.         if (*buf != '<')
  778.         fatal2("< expected at line %ld of patch.\n", p_input_line);
  779.         p_line[i] = savestr(buf+2);
  780.         if (out_of_mem) {
  781.         p_end = i-1;
  782.         return FALSE;
  783.         }
  784.         p_len[i] = strlen(p_line[i]);
  785.         p_char[i] = '-';
  786.     }
  787.     if (hunk_type == 'c') {
  788.         ret = pgets(buf, sizeof buf, pfp);
  789.         p_input_line++;
  790.         if (ret == Nullch)
  791.         fatal2("Unexpected end of file in patch at line %ld.\n",
  792.             p_input_line);
  793.         if (*buf != '-')
  794.         fatal2("--- expected at line %ld of patch.\n", p_input_line);
  795.     }
  796.     Sprintf(buf, "--- %ld,%ld\n", lMin, lMax);
  797.     p_line[i] = savestr(buf);
  798.     if (out_of_mem) {
  799.         p_end = i-1;
  800.         return FALSE;
  801.     }
  802.     p_char[i] = '=';
  803.     for (i++; i<=p_end; i++) {
  804.         ret = pgets(buf, sizeof buf, pfp);
  805.         p_input_line++;
  806.         if (ret == Nullch)
  807.         fatal2("Unexpected end of file in patch at line %ld.\n",
  808.             p_input_line);
  809.         if (*buf != '>')
  810.         fatal2("> expected at line %ld of patch.\n", p_input_line);
  811.         p_line[i] = savestr(buf+2);
  812.         if (out_of_mem) {
  813.         p_end = i-1;
  814.         return FALSE;
  815.         }
  816.         p_len[i] = strlen(p_line[i]);
  817.         p_char[i] = '+';
  818.     }
  819.     }
  820.     if (reverse)            /* backwards patch? */
  821.     if (!pch_swap())
  822.         say1("Not enough memory to swap next hunk!\n");
  823. #ifdef DEBUGGING
  824.     if (debug & 2) {
  825.     int i;
  826.     char special;
  827.  
  828.     for (i=0; i <= p_end; i++) {
  829.         if (i == p_ptrn_lines)
  830.         special = '^';
  831.         else
  832.         special = ' ';
  833.         fprintf(stderr, "%3d %c %c %s", i, p_char[i], special, p_line[i]);
  834.         Fflush(stderr);
  835.     }
  836.     }
  837. #endif
  838.     if (p_end+1 < hunkmax)    /* paranoia reigns supreme... */
  839.     p_char[p_end+1] = '^';  /* add a stopper for apply_hunk */
  840.     return TRUE;
  841.  
  842. malformed:
  843.     fatal3("Malformed patch at line %ld: %s", p_input_line, buf);
  844.         /* about as informative as "Syntax error" in C */
  845.     return FALSE;    /* for lint */
  846. }
  847.  
  848. /* Input a line from the patch file, worrying about indentation. */
  849.  
  850. char *
  851. pgets(bf,sz,fp)
  852. char *bf;
  853. int sz;
  854. FILE *fp;
  855. {
  856.     char *ret = fgets(bf, sz, fp);
  857.     Reg1 char *s;
  858.     Reg2 int indent = 0;
  859.  
  860.     if (p_indent && ret != Nullch) {
  861.     for (s=buf; indent < p_indent && (*s == ' ' || *s == '\t'); s++) {
  862.         if (*s == '\t')
  863.         indent += 8 - (indent % 7);
  864.         else
  865.         indent++;
  866.     }
  867.     if (buf != s)
  868.         Strcpy(buf, s);
  869.     }
  870.     return ret;
  871. }
  872.  
  873. /* Reverse the old and new portions of the current hunk. */
  874.  
  875. bool
  876. pch_swap()
  877. {
  878.     char **tp_line;        /* the text of the hunk */
  879.     short *tp_len;        /* length of each line */
  880.     char *tp_char;        /* +, -, and ! */
  881.     Reg1 LINENUM i;
  882.     Reg2 LINENUM n;
  883.     bool blankline = FALSE;
  884.     Reg3 char *s;
  885.  
  886.     i = p_first;
  887.     p_first = p_newfirst;
  888.     p_newfirst = i;
  889.     
  890.     /* make a scratch copy */
  891.  
  892.     tp_line = p_line;
  893.     tp_len = p_len;
  894.     tp_char = p_char;
  895.     p_line = Null(char**);    /* force set_hunkmax to allocate again */
  896.     p_len = Null(short*);
  897.     p_char = Nullch;
  898.     set_hunkmax();
  899.     if (p_line == Null(char**) || p_len == Null(short*) || p_char == Nullch) {
  900. #ifndef lint
  901.     if (p_line == Null(char**))
  902.         free((char*)p_line);
  903.     p_line = tp_line;
  904.     if (p_len == Null(short*))
  905.         free((char*)p_len);
  906.     p_len = tp_len;
  907. #endif
  908.     if (p_char == Nullch)
  909.         free((char*)p_char);
  910.     p_char = tp_char;
  911.     return FALSE;        /* not enough memory to swap hunk! */
  912.     }
  913.  
  914.     /* now turn the new into the old */
  915.  
  916.     i = p_ptrn_lines + 1;
  917.     if (tp_char[i] == '\n') {        /* account for possible blank line */
  918.     blankline = TRUE;
  919.     i++;
  920.     }
  921.     if (p_efake >= 0) {            /* fix non-freeable ptr range */
  922.     n = p_end - i + 1;
  923.     if (p_efake > i)
  924.         n = -n;
  925.     p_efake += n;
  926.     p_bfake += n;
  927.     }
  928.     for (n=0; i <= p_end; i++,n++) {
  929.     p_line[n] = tp_line[i];
  930.     p_char[n] = tp_char[i];
  931.     if (p_char[n] == '+')
  932.         p_char[n] = '-';
  933.     p_len[n] = tp_len[i];
  934.     }
  935.     if (blankline) {
  936.     i = p_ptrn_lines + 1;
  937.     p_line[n] = tp_line[i];
  938.     p_char[n] = tp_char[i];
  939.     p_len[n] = tp_len[i];
  940.     n++;
  941.     }
  942.     assert(p_char[0] == '=');
  943.     p_char[0] = '*';
  944.     for (s=p_line[0]; *s; s++)
  945.     if (*s == '-')
  946.         *s = '*';
  947.  
  948.     /* now turn the old into the new */
  949.  
  950.     assert(tp_char[0] == '*');
  951.     tp_char[0] = '=';
  952.     for (s=tp_line[0]; *s; s++)
  953.     if (*s == '*')
  954.         *s = '-';
  955.     for (i=0; n <= p_end; i++,n++) {
  956.     p_line[n] = tp_line[i];
  957.     p_char[n] = tp_char[i];
  958.     if (p_char[n] == '-')
  959.         p_char[n] = '+';
  960.     p_len[n] = tp_len[i];
  961.     }
  962.     assert(i == p_ptrn_lines + 1);
  963.     i = p_ptrn_lines;
  964.     p_ptrn_lines = p_repl_lines;
  965.     p_repl_lines = i;
  966. #ifndef lint
  967.     if (tp_line == Null(char**))
  968.     free((char*)tp_line);
  969.     if (tp_len == Null(short*))
  970.     free((char*)tp_len);
  971. #endif
  972.     if (tp_char == Nullch)
  973.     free((char*)tp_char);
  974.     return TRUE;
  975. }
  976.  
  977. /* Return the specified line position in the old file of the old context. */
  978.  
  979. LINENUM
  980. pch_first()
  981. {
  982.     return p_first;
  983. }
  984.  
  985. /* Return the number of lines of old context. */
  986.  
  987. LINENUM
  988. pch_ptrn_lines()
  989. {
  990.     return p_ptrn_lines;
  991. }
  992.  
  993. /* Return the probable line position in the new file of the first line. */
  994.  
  995. LINENUM
  996. pch_newfirst()
  997. {
  998.     return p_newfirst;
  999. }
  1000.  
  1001. /* Return the number of lines in the replacement text including context. */
  1002.  
  1003. LINENUM
  1004. pch_repl_lines()
  1005. {
  1006.     return p_repl_lines;
  1007. }
  1008.  
  1009. /* Return the number of lines in the whole hunk. */
  1010.  
  1011. LINENUM
  1012. pch_end()
  1013. {
  1014.     return p_end;
  1015. }
  1016.  
  1017. /* Return the number of context lines before the first changed line. */
  1018.  
  1019. LINENUM
  1020. pch_context()
  1021. {
  1022.     return p_context;
  1023. }
  1024.  
  1025. /* Return the length of a particular patch line. */
  1026.  
  1027. short
  1028. pch_line_len(line)
  1029. LINENUM line;
  1030. {
  1031.     return p_len[line];
  1032. }
  1033.  
  1034. /* Return the control character (+, -, *, !, etc) for a patch line. */
  1035.  
  1036. char
  1037. pch_char(line)
  1038. LINENUM line;
  1039. {
  1040.     return p_char[line];
  1041. }
  1042.  
  1043. /* Return a pointer to a particular patch line. */
  1044.  
  1045. char *
  1046. pfetch(line)
  1047. LINENUM line;
  1048. {
  1049.     return p_line[line];
  1050. }
  1051.  
  1052. /* Return where in the patch file this hunk began, for error messages. */
  1053.  
  1054. LINENUM
  1055. pch_hunk_beg()
  1056. {
  1057.     return p_hunk_beg;
  1058. }
  1059.  
  1060. #ifndef AMIGA
  1061. /* Apply an ed script by feeding ed itself. */
  1062.  
  1063. void
  1064. do_ed_script()
  1065. {
  1066.     Reg1 char *t;
  1067.     Reg2 long beginning_of_this_line;
  1068.     Reg3 bool this_line_is_command = FALSE;
  1069.     Reg4 FILE *pipefp;
  1070.     FILE *popen();
  1071.  
  1072.     if (!skip_rest_of_patch) {
  1073.     Unlink(TMPOUTNAME);
  1074.     copy_file(filearg[0], TMPOUTNAME);
  1075.     if (verbose)
  1076.         Sprintf(buf, "/bin/ed %s", TMPOUTNAME);
  1077.     else
  1078.         Sprintf(buf, "/bin/ed - %s", TMPOUTNAME);
  1079.     pipefp = popen(buf, "w");
  1080.     }
  1081.     for (;;) {
  1082.     beginning_of_this_line = ftell(pfp);
  1083.     if (pgets(buf, sizeof buf, pfp) == Nullch) {
  1084.         next_intuit_at(beginning_of_this_line,p_input_line);
  1085.         break;
  1086.     }
  1087.     p_input_line++;
  1088.     for (t=buf; isdigit(*t) || *t == ','; t++) ;
  1089.     this_line_is_command = (isdigit(*buf) &&
  1090.       (*t == 'd' || *t == 'c' || *t == 'a') );
  1091.     if (this_line_is_command) {
  1092.         if (!skip_rest_of_patch)
  1093.         fputs(buf, pipefp);
  1094.         if (*t != 'd') {
  1095.         while (pgets(buf, sizeof buf, pfp) != Nullch) {
  1096.             p_input_line++;
  1097.             if (!skip_rest_of_patch)
  1098.             fputs(buf, pipefp);
  1099.             if (strEQ(buf, ".\n"))
  1100.             break;
  1101.         }
  1102.         }
  1103.     }
  1104.     else {
  1105.         next_intuit_at(beginning_of_this_line,p_input_line);
  1106.         break;
  1107.     }
  1108.     }
  1109.     if (skip_rest_of_patch)
  1110.     return;
  1111.     fprintf(pipefp, "w\n");
  1112.     fprintf(pipefp, "q\n");
  1113.     Fflush(pipefp);
  1114.     Pclose(pipefp);
  1115.     ignore_signals();
  1116.     if (move_file(TMPOUTNAME, outname) < 0) {
  1117.     toutkeep = TRUE;
  1118.     chmod(TMPOUTNAME, filemode);
  1119.     }
  1120.     else
  1121.     chmod(outname, filemode);
  1122.     set_signals();
  1123. }
  1124. #endif
  1125.